//蓝牙打印
// import bluetooth from '/pages/profile/utils/bluetoothObfuscated.js';
// import util from '/pages/profile/utils/util.js';
// import pos from '/pages/profile/utils/pos.js';

// services
const Host = require('common/services/hosts')
const Dave = require('dave/dave.min.js')

Dave.config({
    apiHost: Host.apiHost,
    fussHost: Host.fuss10,
})
const { User, Pay, ApiCreater, Ubt, HashToUrl } = Dave
// const CartService = require('common/services/cart.js')

const setSceneAndQrcodeForUbt = (options) => {
    // 用来方便线上看到 log 信息hyos
    // 从报错中看到 options 可能为空，但不知道什么情况下会这样。所以这里判断一次
    if (!options) {
        return
    }
    // 存储场景值和二维码参数，Ubt 每次发送 PV 的时候都需要带上这两个参数
    let qrcode = options.query.qrcode
    if (qrcode) {
        wx.setStorageSync('qrcode', qrcode)
        Ubt.from = qrcode
    }
    let scene = options.scene
    wx.setStorageSync('scene', scene)
    Ubt.scene = scene
}

// 引入 polyfill
Object.assign = Object.assign || require('./libs/object-assign')
Array.prototype.find = Array.prototype.find || require('./libs/array-find')

App({
    extend(objs, overwrite = false) {
        var newbee = {}
        objs.forEach(obj => {
            for (let i in obj) {
                if (obj.hasOwnProperty(i)) {
                    if (newbee.hasOwnProperty(i) && !overwrite) {
                        if ( typeof newbee[i] === 'object'
                            && typeof obj[i] === 'object') {
                            if (Object.prototype.toString.call(obj[i]) !== '[object Array]'
                                && Object.prototype.toString.call(newbee[i]) !== '[object Array]') {
                                newbee[i] = this.extend([newbee[i], obj[i]])
                            }
                        } else if ( typeof newbee[i] === 'function'
                            && typeof obj[i] === 'function'
                            && ['onLoad', 'onReady', 'onShow', 'onHide', 'onUnload', 'onPullDownRefresh', 'onReachBottom'].indexOf(i) > -1) {
                            var last = newbee[i]
                            newbee[i] = function (options) {
                                last.call(this, options)
                                obj[i].call(this, options)
                            }
                        }
                    } else {
                        newbee[i] = obj[i]
                    }
                }
            }
        })
        return newbee
    },
    overwrite(target, objs) {
        objs.forEach(obj => {
            for (let i in obj) {
                if (obj.hasOwnProperty(i)) {
                    target[i] = obj[i]
                }
            }
        })
        return target
    },

    checkSession() {
        return new Promise((resolve, reject) => {
            wx.checkSession({
                success: resolve,
                fail: reject,
            })
        })
    },

    onLaunch(options) {
        setSceneAndQrcodeForUbt(options)
        // 在每次 launch 的时候 checkSession 来确保现在用户的登录状态始终是 ok 的
        this.checkSession()
        // //调用API从本地缓存中获取数据
        // bluetooth.OpenPrint();//打开打印机
        // wx.setStorageSync('gotoSellOutGood','true')
    },

    onShow(options) {
        setSceneAndQrcodeForUbt(options)
    },

    services: {
        User, Ubt, Pay, ApiCreater, HashToUrl,
        // libs 和 utils 没区别？
        // SOCKET: require('./libs/socket.js'),
        Geohash: require('./common/utils/geohash.js'),
        Promise: require('./libs/promise.js'),
        paramsToString: require('./common/utils/paramsToString.js'),
        webCart: require('./libs/web-cart.js').default,
        Base64: require('./libs/base64.js'),
        API: require('./common/utils/api.js'),
        weixinUserInfo: require('./common/services/weixinUserInfo.js'),
    }
})
